package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.FileNotFoundException;
import java.util.List;

@RestController
@RequestMapping("/depts")//把方法 路径进行提取
@Slf4j // 日志信息打印  当前程序日志
public class DeptController {

    @Autowired
    private DeptService deptService;

//    @RequestMapping(method = RequestMethod.GET)
    @GetMapping//等同于上面这句话
    public Result list(){
        //需要的数据去service查询
         log.info("接收了前端查询请求");
        System.out.println("接收了前端查询请求");
        List<Dept> deptList = deptService.list();
        log.info("查询到的数据是:"+deptList);

        return Result.success(deptList);
    }

    // 完成跟据id删除指定部门
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) throws FileNotFoundException {
        log.info("获取到删除的id是:"+id);
        //调用service完成删除
        deptService.delete(id);//现在爆红没事 我需要
                 //我需要 我就alt+enter
        return Result.success();
    }

//    @RequestMapping(method = RequestMethod.POST)
    @PostMapping
    public Result save(@RequestBody Dept dept){//json格式数据用对象接收
        log.info("接收到对象是:"+dept);
        //调用 service完成添加
        deptService.add(dept);//添加要传递参数
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查看查询的id:"+id);
        // 找service查询
       Dept dept =  deptService.findById(id);
       return Result.success(dept);
    }

    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("查看修改的信息:"+dept);
        log.info("查看修改的信息:{}",dept);
        // 找service
        deptService.update(dept);

        return Result.success();
    }
}
